package com.jinzhi.jzweb.strategy.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.pojo.AsourVo;
import com.jinzhi.jzweb.pojo.BsourVo;
import com.jinzhi.jzweb.pojo.VtatisticsVo;
import com.jinzhi.jzweb.service.ExaminationBatchComSService;
import com.jinzhi.jzweb.service.ExaminationResultsComService;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.jzweb.strategy.BaseStrategy;
import com.jinzhi.jzweb.strategy.factory.SourConfig;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.sys.service.SysUserRelevancyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;


import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/*
 *  @author : JingChangTian
 *  @date   : 2023/8/11 20:24
 *  @description :  A赛场
 *
 */
@Component(value = "B")
public class ExaminationBSour extends BaseController implements BaseStrategy  {

    private final StuSignUpComService stuSignUpService;


    private final ExaminationResultsComService examinationResultsComService;


    public ExaminationBSour(StuSignUpComService stuSignUpService,
                            ExaminationResultsComService examinationResultsComService) {
        this.stuSignUpService = stuSignUpService;
        this.examinationResultsComService = examinationResultsComService;
    }

    /**
     * A赛场分数统计业务层
     * @param examinationAnswerSheetComDTO
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    @Override
    public Result<VtatisticsVo> list(ExaminationResultsComDO examinationAnswerSheetComDTO) throws NoSuchFieldException, IllegalAccessException {
        VtatisticsVo vtatisticsVo = new VtatisticsVo();
        return Result.ok(vtatisticsVo);
    }

    @Override
    public Page<StuSignUpComDO> getStuSignByCourt2(String id,Integer curr,Integer pageSize) {
        Page<StuSignUpComDO> page = new Page<>(
                Optional.of(curr).orElse(1),
                Optional.of(pageSize).orElse(10));
        //根据赛场id查询考生信息，理论赛场查询全部
        Page<StuSignUpComDO>  sign = stuSignUpService.selectPage(page,new
                EntityWrapper<StuSignUpComDO>().
                isNotNull("num2").
                eq("cbatch_id",id).
                orderBy("num2", true));
        //根据学生id查询成绩
        ExaminationResultsComDO examinationResultsComDO = new ExaminationResultsComDO();
        sign.getRecords().forEach(a ->{
            examinationResultsComDO.setStuSignId(a.getId());
            a.setExaminationResultsComDO(examinationResultsComService.selectOne(examinationResultsComDO));
        });
        return sign;
    }

    @Override
    public String pageReturn() {
        return "jzweb/transcribe/recordpointsB";
    }

    @Override
    public String markReturn(Integer type) {
        if(type == 1){
            return "jzweb/transcribe/gradeB";
        }else if(type == 2){
            return "jzweb/transcribe/gradeBPo";
        }
       return null;
    }

    @Override
    public void downloadImportTemplate(HttpServletResponse response, String id) {
        List<BsourVo> bsourVos = new ArrayList<>();
        //根据赛场id查询考生信息，理论赛场查询全部
        List<StuSignUpComDO> sign = stuSignUpService.selectList(new EntityWrapper<StuSignUpComDO>().
                isNotNull("num2").
                eq("cbatch_id",id).
                orderBy("num2", true));
        sign.stream().sorted(Comparator.comparing(StuSignUpComDO::getCourt2,Comparator.nullsLast(String::compareTo)).
                thenComparing(StuSignUpComDO::getRealityNm1,Comparator.nullsLast(String::compareTo))).
                collect(Collectors.toList());

        //根据学生id查询成绩
        ExaminationResultsComDO examinationResultsComDO = new ExaminationResultsComDO();
        sign.forEach(a ->{
            BsourVo bsourVo = new BsourVo();
            examinationResultsComDO.setStuSignId(a.getId());
            ExaminationResultsComDO rcd = examinationResultsComService.selectOne(examinationResultsComDO);
            if(rcd!=null) {
                bsourVo.setRemarkB(rcd.getRemarkA());
                bsourVo.setScoingb1(rcd.getScoingb1());
                bsourVo.setScoingb2(rcd.getScoingb2());
                bsourVo.setScoingb3(rcd.getScoingb3());
                BigDecimal pjf = new BigDecimal( (rcd.getScoingb1() + rcd.getScoingb2() + rcd.getScoingb3()) /3);
                Double pjff = pjf.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                bsourVo.setPjf(pjff);
            }
            bsourVo.setNum2(a.getNum2());
            bsourVos.add(bsourVo);
        });
        //导出操作
        EasyPoiUtil.exportExcel(bsourVos, "案例分析成绩导出", "案例分析成绩导出", BsourVo.class, "案例分析成绩导出", response);
    }

}
